{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Documentation\n",
    "\n",
    "Due to constraints on the length of this book, we cannot possibly introduce every single PyTorch function and class (and you probably would not want us to).\n",
    "\n",
    "## Finding all the functions and classes in the module\n",
    "\n",
    "In order to know which functions and classes can be called in a module, we invoke the `dir` function. For instance, we can query all properties in the `nd.random` module as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__text_signature__']\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "print(dir(torch.tensor))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generally, we can ignore functions that start and end with `__` (special objects in Python) or functions that start with a single `_`(usually internal functions). Based on the remaining function/attribute names, we might hazard a guess that this module offers various methods for generating random numbers, including sampling from the uniform distribution (`uniform`), normal distribution (`normal`), and Poisson distribution  (`poisson`)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finding the usage of specific functions and classes\n",
    "\n",
    "For more specific instructions on how to use a given function or class, we can invoke the  `help` function. As an example, let's explore the usage instructions for torch's `ones_like` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function ones_like:\n",
      "\n",
      "ones_like(...)\n",
      "    ones_like(input, dtype=None, layout=None, device=None, requires_grad=False) -> Tensor\n",
      "    \n",
      "    Returns a tensor filled with the scalar value `1`, with the same size as\n",
      "    :attr:`input`. ``torch.ones_like(input)`` is equivalent to\n",
      "    ``torch.ones(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)``.\n",
      "    \n",
      "    .. warning::\n",
      "        As of 0.4, this function does not support an :attr:`out` keyword. As an alternative,\n",
      "        the old ``torch.ones_like(input, out=output)`` is equivalent to\n",
      "        ``torch.ones(input.size(), out=output)``.\n",
      "    \n",
      "    Args:\n",
      "        input (Tensor): the size of :attr:`input` will determine size of the output tensor\n",
      "        dtype (:class:`torch.dtype`, optional): the desired data type of returned Tensor.\n",
      "            Default: if ``None``, defaults to the dtype of :attr:`input`.\n",
      "        layout (:class:`torch.layout`, optional): the desired layout of returned tensor.\n",
      "            Default: if ``None``, defaults to the layout of :attr:`input`.\n",
      "        device (:class:`torch.device`, optional): the desired device of returned tensor.\n",
      "            Default: if ``None``, defaults to the device of :attr:`input`.\n",
      "        requires_grad (bool, optional): If autograd should record operations on the\n",
      "            returned tensor. Default: ``False``.\n",
      "    \n",
      "    Example::\n",
      "    \n",
      "        >>> input = torch.empty(2, 3)\n",
      "        >>> torch.ones_like(input)\n",
      "        tensor([[ 1.,  1.,  1.],\n",
      "                [ 1.,  1.,  1.]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(torch.ones_like)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From the documentation, we can see that the `ones_like` function creates a new array with the same shape as the supplied tensor and all elements set to `1`. Whenever possible, you should run a quick test to confirm your interpretation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1.],\n",
       "        [1., 1., 1.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([[0, 0, 0], [2, 2, 2]], dtype = torch.float)\n",
    "y = torch.ones_like(x)\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the Jupyter notebook, we can use `?` to display the document in another window. For example, `torch.randn?` will create content that is almost identical to `help(torch.randn)`, displaying it in a new browser window. In addition, if we use two question marks, e.g. `torch.randn??`, the code implementing the function will also be displayed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## API Documentation\n",
    "\n",
    "For further details on the API details check the PyTorch website at  [https://pytorch.org](https://pytorch.org). You can find the details under the appropriate headings (also for programming languages other than Python)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise\n",
    "\n",
    "Look up `ones_like` and `autograd` in the API documentation."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
